<template>
	<view>
		<!-- #ifdef H5 -->
		<iframe class="iframe_test" id="iframe_test" :style="{'height':vheight + 'px'}" :src="htmlUrl"> </iframe>
		<!-- #endif -->
		<!-- #ifndef H5 -->
		<web-view :src="htmlUrl" @message='message'></web-view>
		<!-- #endif -->
	</view>
</template>

<!-- 小程序url传参，app,h5通信传参 -->
<script>
	import {
		mapGetters
	} from "vuex"

	export default {
		data() {
			return {
				htmlUrl: '',
				vheight: '',
				pageConfig: {
					title: '',
				}
			}
		},
		computed: {
			...mapGetters(["userinfo", "token"])
		},
		onReady() {
			let _this = this;
			uni.setNavigationBarTitle({
				title: this.pageConfig.name
			})
			uni.getSystemInfo({
				success(res) {
					_this.vheight = res.windowHeight;
				}
			});
		},
		mounted() {
			this.$nextTick(() => {
				// #ifdef H5
				this.sendUniappParamsByH5()
				//h5接收参数
				let me = this
				window.addEventListener("message", function(event) {
					if (event.data && event.data.type == '') {}
				}, false);
				// #endif
			})
		},
		onLoad(e) {
			this.pageConfig = e.pageConfig ? JSON.parse(e.pageConfig) : {}
			// #ifdef APP-PLUS
			let url = '/hybrid/html/Camera/index.html'
			// #endif
			// #ifdef H5
			let url = './hybrid/html/Camera/index.html' //本项目打包表单打开地址
			// #endif
			this.htmlUrl = url + '?pageConfig=' + encodeURIComponent(JSON.stringify(this.pageConfig))
			console.log(this.htmlUrl, '网页', this.pageConfig)
		},
		methods: {
			//h5发送参数
			sendUniappParamsByH5() {
				let me = this
				let iframe = document.getElementById('iframe_test');
				iframe.onload = function() {
					let _data = {
						uniappParams: ''
					}
					iframe.contentWindow.postMessage({
						msgFromUniapp: JSON.stringify(_data)
					}, "*");
				}
			},
			//app接收参数
			message(event) {
				let customEventType = event.detail.data[0].type
				// #ifndef H5
				if (customEventType == 'refreshData') {
					uni.reLaunch({
						url: "/pages/PublicPage/PublicPage?pageConfig=" + JSON.stringify({
							page: 'MeasurementResult',
							name: '测量结果',
						})
					})
				}
				// #endif
			}
		}
	}
</script>

<style scoped lang="scss">
	.iframe_test {
		width: 100%;
		border: none;
	}
</style>